package com.example.handheld4_gsignal_app.services;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;

/**
 * 位置服务类 - 处理GPS位置获取
 */
public class LocationService implements LocationListener {
    
    private Context context;
    private LocationManager locationManager;
    private LocationCallback callback;

    public interface LocationCallback {
        void onLocationUpdate(Location location);
        void onLocationError(String error);
    }

    public LocationService(Context context) {
        this.context = context;
        this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    /**
     * 开始位置监听
     */
    public void startLocationUpdates(LocationCallback callback) {
        this.callback = callback;
        
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) 
                != PackageManager.PERMISSION_GRANTED) {
            if (callback != null) {
                callback.onLocationError("需要位置权限");
            }
            return;
        }

        try {
            // 请求位置更新
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, this);
        } catch (Exception e) {
            if (callback != null) {
                callback.onLocationError("位置服务启动失败: " + e.getMessage());
            }
        }
    }

    /**
     * 停止位置监听
     */
    public void stopLocationUpdates() {
        if (locationManager != null) {
            locationManager.removeUpdates(this);
        }
    }

    /**
     * 获取最后已知位置
     */
    public Location getLastKnownLocation() {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) 
                != PackageManager.PERMISSION_GRANTED) {
            return null;
        }

        Location gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (gpsLocation != null && networkLocation != null) {
            return gpsLocation.getTime() > networkLocation.getTime() ? gpsLocation : networkLocation;
        } else if (gpsLocation != null) {
            return gpsLocation;
        } else {
            return networkLocation;
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        if (callback != null) {
            callback.onLocationUpdate(location);
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // 位置提供者状态变化
    }

    @Override
    public void onProviderEnabled(String provider) {
        // 位置提供者启用
    }

    @Override
    public void onProviderDisabled(String provider) {
        if (callback != null) {
            callback.onLocationError("位置提供者已禁用: " + provider);
        }
    }
}